﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SeleniumRecorder.HelperClass;
namespace SeleniumRecorder
{
    /// <summary>
    /// Interaction logic for SetProperties.xaml
    /// </summary>
    public partial class SetProperties : Window
    {
        private ControlDataStructure data = null;
        public SetProperties(ControlDataStructure controlData)
        {

            InitializeComponent();
            this.Topmost = true;
            data = controlData;

            foreach (var v in PropCombo.Items)
            {
                var item = v as ComboBoxItem;
                if (item.Content.ToString().ToString() == controlData.SerachBy.ToString())
                {
                    item.IsSelected = true;
                    break;
                }
            }

            SearchValue.Text = controlData.SearchValue;
            FieldName.Text = controlData.FieldName;
            Parent.Text = controlData.parent;

        }

        private void PropCombo_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if ((PropCombo.SelectedItem as ComboBoxItem).Content.ToString() == "CSSSelector")
            {
                CSSCombo.Visibility = Visibility.Visible;
                CSSComboLabel.Visibility = Visibility.Visible;
                CSSCombo.Items.Clear();
                if (data.attributes.Count() > 0)
                {
                    foreach (var ite in data.attributes)
                    {
                        CSSCombo.Items.Add(ite.Key);
                    }
                }
                else
                {
                    CSSCombo.Items.Add("No Property");
                }

            }
            else
            {

                string selectedValue = (PropCombo.SelectedItem as ComboBoxItem).Content.ToString();
                CSSCombo.Visibility = Visibility.Collapsed;
                CSSComboLabel.Visibility = Visibility.Collapsed;

                switch (selectedValue)
                {
                    case "ID":
                        SearchValue.Text = data.ID;
                        break;

                    case "Name":
                        SearchValue.Text = data.Name;
                        break;

                    case "ClassName":
                        SearchValue.Text = data.ClassName;
                        break;

                    case "TagName":
                        SearchValue.Text = data.TagName;
                        break;

                    case "CSSSelector":
                        SearchValue.Text = data.CssSelectorValue;
                        break;
                    case "PartialLinkText":
                        SearchValue.Text = data.PartialLink;
                        break;
                    case "LinkText":
                        SearchValue.Text = data.LinkText;
                        break;
                    case "XPath":
                        SearchValue.Text = data.XpathValue;
                        break;

                }
            }
        }

        private void CSSCombo_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            string selectedValue = (CSSCombo.SelectedItem).ToString();

            if (data.attributes.ContainsKey(selectedValue))
            {

                if (selectedValue.Equals("custom"))
                {
                    SearchValue.Text = data.attributes[selectedValue]; 
                }
                else
                {
                    SearchValue.Text = data.TagName + "[" + selectedValue + "='" + data.attributes[selectedValue] + "']"; 
                }
            }
        }

        private void Button_Click_Save(object sender, RoutedEventArgs e)
        {
            if (SearchValue.Text == "")
            { 
                MessageBox.Show("Search Value can't be empty !");
                return;
            }

            string selectedValue = (PropCombo.SelectedItem as ComboBoxItem).Content.ToString();
            if (selectedValue == "")
            {
                MessageBox.Show("Please select search property");
                return;
            }

            data.SerachBy = selectedValue.ToEnum<SearchBY>(); 

            data.FieldName = FieldName.Text;
            data.parent = Parent.Text;

            this.Close();
        }

        private void Button_Click_Cancel(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnClose_Click_Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
